package com.wjy.simple.fragment;

import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wjy.sfhcore.engine.BaseAsyncTaskLoader;
import com.wjy.sfhcore.ui.fragment.list.BaseListAdapter;
import com.wjy.sfhcore.ui.fragment.list.BaseListFragment;
import com.wjy.simple.R;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;

/**
 * Created by wangjingyuan on 15/6/2.
 */
public class MeFragment extends BaseListFragment<Object, List<Object>> {

    private View rootView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_list, container, false);
        }
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mToolBar.setTitle("列表");
        if (isFirstLoad) {
            loadData();
            isFirstLoad = false;
        }
    }

    @Override
    protected void loadFinished(int action, List<Object> result) {
        if (result == null) {
            changeViewState(LoadResult.LOAD_FAIL);
        } else {
            changeViewState(LoadResult.LOAD_SUCCESS);
            setTotalPages(10);
            setDatas(result);
        }
    }


    @Override
    public Loader onCreateLoader(int action, Bundle bundle) {
        return new BaseAsyncTaskLoader<List<Object>>(baseActivity) {
            @Override
            public List<Object> loadInBackground() {
                SystemClock.sleep(2000);
                List<Object> data = new ArrayList<>();
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                data.add(new Object());
                return data;
            }
        };
    }


    @Override
    protected BaseListAdapter<Object> getAdapter() {
        return new BaseListAdapter<Object>(baseActivity) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = View.inflate(context, android.R.layout.simple_expandable_list_item_1, null);
                }
                ((TextView) convertView).setText(position + "item");
                return convertView;
            }
        };
    }

    @Override
    protected void getDatas() {
        getLoaderManager().restartLoader(0, null, this);
    }

    @Override
    public boolean isShowBackButton() {
        return false;
    }
}
